import 'package:flutter/material.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/assessment/questionRecordInfoResult.dart';
import 'package:ichinesereader/models/book/BookItem.dart';
import 'package:ichinesereader/utils/log.dart';
import '../../models/assessment/questionRecordSection.dart';
import '../common/bookQuestionCollapse.dart';

class BookGrammar extends StatefulWidget {
  BookGrammar(
      {Key? key, required this.bookItem, this.readingType, this.submited})
      : super(key: key);
  final BookItem bookItem;
  final NanhaiBookReadingTypes? readingType;
  final bool? submited;
  static const String routeName = '/book/quickreview';

  @override
  State<BookGrammar> createState() => _BookGrammarState();
}

class _BookGrammarState extends State<BookGrammar> {
  final AssessmentsController _assessmentsController = Get.find();
  List<QuestionRecordSection> grammarSections = [];
  var recordStatus;
  @override
  void initState() {
    List<QuestionRecordSection>? result;
    if (widget.submited ?? false) {
      QuestionRecordInfo? result = _assessmentsController.questionRecordInfo;
      if (result != null) {
        recordStatus = result.recordStatus;
        if (result.recordStatus != null) {
          recordStatus = result.recordStatus!;
        }
        setState(() {
          grammarSections =
              result.list!.where((i) => i.questions?.length != 0).toList();
        });
      }
    } else {
      result = _assessmentsController.grammarRecordInfo;
      if (result != null) {
        // recordStatus = result.recordStatus;
        // there is no result.recordStatus yet -- to do later
        recordStatus = 'UNDONE';
        // for (final data in grammarSections) {
        //   print(data.questions?.length == 0);
        // }
        setState(() {
          // some questions demo data are null currently
          grammarSections =
              result!.where((i) => i.questions?.length != 0).toList();
        });
      }
    }

    super.initState();
  }

  @override
  void dispose() {
    //Get.delete<AssessmentsController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build grammar');
    return Scaffold(
      appBar: AppBar(
        title: Container(
          width: double.infinity,
          height: 40,
          child: Center(
            child: Text(this.widget.bookItem.bookTitle.toString()),
          ),
        ),
      ),
      body: grammarSections.length > 0
          ? BookQuestionCollapse(
              bookcode: '${widget.bookItem.bookCode}',
              recordStatus: recordStatus,
              bookItem: widget.bookItem,
              readingType: widget.readingType,
              nanhaiBookKnowledgeTypes: NanhaiBookKnowledgeTypes.grammar,
              bookQuestionCollapseSections: grammarSections,
              kind: "GRAMMAR",
              submited: widget.submited)
          : Text('Grammar data coming soon'.tr),
    );
  }
}
